/*******************************************************************
 *
 * @Version : 0.5 
 *
 * SceneMessageProcessor.java 
 *
 * 2011-10-13 下午05:05:16
 *
 *******************************************************************/
package game.breeze.message.processor;

import game.breeze.Breeze;
import game.breeze.astar.AStarNode;
import game.breeze.data.c.Monster;
import game.breeze.message.IMessageProcessor;
import game.breeze.message.SceneMessageCode;
import game.breeze.screen.GameScreen;
import game.breeze.socket.util.ByteBuffer;

import java.util.ArrayList;
import java.util.List;

/**
 * Class desc there
 * 
 * @author yangwei ywengineer@gmail.com
 * 
 */
public class SceneMessageProcessor implements IMessageProcessor {
	private static final SceneMessageProcessor instance = new SceneMessageProcessor();

	private SceneMessageProcessor() {

	}

	public static final SceneMessageProcessor getInstance() {
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * game.breeze.message.IMessageProcessor#proccessMessage(game.breeze.Breeze,
	 * game.breeze.socket.util.ByteBuffer)
	 */
	@Override
	public void proccessMessage(Breeze game, ByteBuffer buffer) {
		int type = buffer.readInt();
		switch (type) {
		case SceneMessageCode.MSG_SCENE_PLAYER_ADDED:
			System.out.println(buffer.readUTF());
			System.out.println(buffer.readUTF());
			System.out.println(buffer.readInt());
			System.out.println(buffer.readInt());
			break;
		case SceneMessageCode.MSG_SCENE_MONSTER:
			processMonster(game, buffer);
			break;
		case SceneMessageCode.MSG_SCENE_PLAYER_FIND_PATH:
			processFindPath(game, buffer);
			break;
		default:
			break;
		}
	}

	private void processMonster(Breeze game, ByteBuffer buffer) {
		int size = buffer.readInt();
		if (game.getScreen() instanceof GameScreen) {
			GameScreen screen = (GameScreen) game.getScreen();
			List<Monster> ms = new ArrayList<Monster>();
			for (int i = 0; i < size; i++) {
				Monster m = new Monster();
				m.readFrom(buffer);
				ms.add(m);
			}
			screen.onMonsterStatus(ms);
		}
	}

	private void processFindPath(Breeze game, ByteBuffer buffer) {
		int size = buffer.readInt();
		if (game.getScreen() instanceof GameScreen) {
			GameScreen screen = (GameScreen) game.getScreen();
			List<AStarNode> path = new ArrayList<AStarNode>();
			if (size == 0) {
				screen.addTipMessage("Can not find path!!!!!!!!");
			} else {
				for (int i = 0; i < size; i++) {
					int row = buffer.readInt();
					int col = buffer.readInt();
					AStarNode node = new AStarNode(row, col, null);
					path.add(node);
				}
				screen.player.walk(path);
			}
		}
	}
}
